<?php
/**
 +------------------------------------------------------------------------------
 * CommonAction控制器类
 +------------------------------------------------------------------------------
 * @category   Common
 * @package  index
 * @subpackage  index
 * @author   朝闻道 <hydata@gmail.com>
 * @date  2010-8-22
 * @time  下午12:48:57
 +------------------------------------------------------------------------------
 */
class CommonAction extends Action {
	protected $lang = '';
	protected $plang = array ();
	protected $user=array();
	protected function _initialize() {
		header ( "Content-Type:text/html; charset=utf-8" );
		import ( "ORG.Util.Page" );
		import ( "@.Com.ZQPage" );
		import ( "ORG.Util.Input" );
		$this->lang = cookie ( 'think_language' );
		load ( "extend" );
		switch ($this->lang) {
			case 'en-us' :
				$this->plang ['guo'] = 'en';
				$this->plang ['l'] = 'e';
				$this->plang ['t'] = 'En';
				$this->plang ['all'] = 'en-us';
				break;
			case 'zh-cn' :
				$this->plang ['guo'] = 'cn';
				$this->plang ['l'] = 'c';
				$this->plang ['t'] = 'Cn';
				$this->plang ['all'] = 'zh-cn';
				break;
			default :
				$this->plang ['guo'] = 'en';
				$this->plang ['l'] = 'e';
				$this->plang ['t'] = 'En';
				$this->plang ['all'] = 'en-us';
				break;
		}
		$this->assign ( 'plang', $this->plang );
		$this->assign ( 'left_one', $this->get_cat_tree ( 1 ) );
		$this->assign ( 'left_two', $this->get_cat_tree ( 2 ) );
		if ($_GET ['clear'] == 'bfc') {
			clearCache ( 0 );
			clearCache ( 1 );
			clearCache ( 2 );
			clearCache ( 3 );
		}
		$this->user=self::_is_login();
		if($this->user){
			$this->assign('user',$this->user);
		}
	}
	
	/**
	 * @检查是否登录
	 * @author   朝闻道 <hydata@gmail.com>
	 * @date:  2010-8-22 / time:  下午01:03:42
	 * @return array or bool
	 */
	protected function _is_login() {
		//_is_login_检查是否登录
		if (Session::is_set ( 'info' )) {
			return Session::get ( 'info' );
		} else
			return false;
	} //end _is_login_检查是否登录
	

	/**
	 *获取大类下的子类及三级分类
	 *@date 2010-8-24
	 *@time 下午03:22:52
	 *@param topid int 类别ID
	 */
	protected function get_cat_tree($topid) {
		//获取大类下的子类及三级分类
		$dao = D ( "Cat" );
		$condition = array ();
		$condition ['topid'] = $topid;
		$condition ['is_show'] = 1;
		$data = $dao->where ( $condition )->order ( "id ASC" )->findAll ();
		//dump($data);
		$arr = array ();
		foreach ( $data as $v ) {
			$v ['title'] = $v [$this->plang ['l'] . 'name'];
			if ($topid != $v ['pid']) {
				$arr [$v ['pid']] ['_son'] [] = $v;
			} else {
				$arr [$v ['id']] = $v;
			}
		}
		return $arr;
	} //end get_cat_tree
	

	/**
	 *根据栏目ID生成导航
	 *@date 2010-8-27
	 *@time 上午10:39:35
	 *@param cid int 类别ID
	 *@param l string 语言的首字母
	 */
	protected function cat_dh($cid, $l) {
		//根据栏目ID生成导航
		$dao = D ( "Cat" );
		$dhtext = ''; //L("_ally_web_directory_").' >> ';
		$cat = $dao->where ( "id=$cid" )->find ();
		if ($cat ['topid'] != 0) {
			$top = $dao->where ( "id={$cat['topid']}" )->find ();
			$dhtext .= "<a href='".ru("List/index",array('id'=>$top['id']))."'>" . $top [$l . 'name'] . '</a> >> ';
		}
		if ($cat ['pid'] != 0 && $cat ['topid'] != $cat ['pid']) {
			$p = $dao->where ( "id={$cat['pid']}" )->find ();
			$dhtext .= "<a href='".ru("List/index",array('id'=>$p['id']))."'>" . $p [$l . 'name'] . '</a> >> ';
		}
		return $dhtext . "<a href='".ru("List/index",array('id'=>$cat['id']))."'>" . $cat [$l . 'name'] . '</a>';
	} //end cat_dh
	

	/**
	 * 按天统计资源点击量
	 * @param xid int 资源ID
	 * @param type string 资源类别
	 */
	protected static function iicstat($xid, $type = 'arc') {
		$m = new Model ();
		$type = empty ( $type ) ? MODULE_NAME : $type;
		$mon = mktime ( 0, 0, 0, date ( 'n' ), 1 );
		$d = 'd' . date ( 'd' );
		$num = '1';
		if ($type == 'arc') {
			$num = ($xid ['activetime'] + 300) > $t ? '6' : 1;
			$num = $xid ['vipstart'] < $t && $xid ['vipend'] > $t ? '6' : 1;
			$xid = $xid ['id'];
		}
		$sql = "INSERT INTO `iic_stat` (`id`, `xid`, `mon`, `stype`, `$d`) VALUES (NULL, '$xid', '$mon', '$type', '$num') ON DUPLICATE KEY UPDATE `$d`=`$d`+$num;";
		return $m->execute ( $sql );
	} // END iicstart
	
	/**
	 * 
	 * 获取点击量最高的资源 TOPn
	 * @param day int 天数
	 * @param type string 类别 默认 arc
	 * @param limit string 查询的条数
	 */
	protected static function _top_stat($day = '7',$type='arc' ,$limit = "0,1") {
		//每日之星
		$m = new Model ();
		$t = time ();
		$d7 = mktime ( 0, 0, 0, date ( 'n',$t ), date ( 'd',$t ), date ( 'Y',$t ) ) - 60 * 60 * 24 * $day; //当前时间的前7天的秒数
		//$d7=mktime(0, 0, 0, 03, 03,date('Y'))-60*60*24*7;
		$mon = mktime ( 0, 0, 0, date ( 'n',$t ), 1, date ( 'Y',$t ) ); //当前月份的秒数
		//$mon=mktime(0, 0, 0, 03, 03,date('Y'));
		$mon2 = mktime ( 0, 0, 0, date ( 'n', $d7 ), 1, date ( 'Y',$t ) ); //当前时间的前7天的秒数的月份的秒数
		//$mon2=mktime(0, 0, 0, date('n',$d7),1,date('Y'));
		$field = '';
		if ($mon == $mon2) { //检查是否同月
			for($i = 1; $i < 8; $i ++) {
				$dt = $t - (60 * 60 * 24) * $i;
				$field .= '`d' . date ( 'd', $dt ) . '`+';
			}
			$field = trim ( $field, '+' );
			$sql = "SELECT xid,stype,$field d FROM `iic_stat` WHERE (`stype`='$type' AND `mon`='$mon') AND `is_status`=1 ORDER BY d DESC LIMIT $limit";
		} else { //不同月需要查询两个表
			$field2 = '';
			for($i = 1; $i < $day+1; $i ++) {
				$dt = $t - (60 * 60 * 24) * $i;
				if (date ( 'd', $dt ) > date ( 'd' )) {
					$field2 .= 'mm.`d' . date ( 'd', $dt ) . '`+';
				} else {
					$field .= 'm.`d' . date ( 'd', $dt ) . '`+';
				}
			}
			$field = trim ( $field . $field2, '+' );
			$sql = "SELECT m.xid xid,m.stype stype,$field d FROM `iic_stat` as m LEFT JOIN `iic_stat` AS mm ON m.xid=mm.xid AND m.stype=mm.stype WHERE (m.`stype`='$type' AND (m.`mon`='$mon' or mm.`mon`='$mon2') AND m.id<>mm.id) AND (m.`is_status`=1 AND mm.`is_status`=1) ORDER BY d DESC LIMIT $limit";
		}
		//$list=$m->query ( $sql );
		return $m->query ( $sql );
	} //end _top_stat
	/**
	 *展示次数或点击量统计
	 *@date 2010-9-10
	 *@time 上午10:33:46
	 *@param array $arr 资源列表 id 活跃时间 vip开始和结束时间
	 *@param int $click 1:更新点击  2:更新展示
	 */
	protected static function clickorshow($arr=array(), $click = '1') {
		//展示次数或点击量统计
		$arc = new Model ( 'Arc' );
		$t = time ();
		if ($click != '1') {
			foreach ( $arr as $v ) {
				$num = ($v ['activetime'] + 300) > $t ? rand ( 1, 4 ) : 1;
				$num = $v ['vipstart'] < $t && $v ['vipend'] > $t ? rand ( 1, 6 ) : 1;
				$arc->setInc ( 'shownum', "id={$v['id']}", $num );
			}
		} else {
			$num = ($arr ['activetime'] + 300) > $t ? rand ( 5, 10 ) : 1;
			$num = $arr ['vipstart'] < $t && $arr ['vipend'] > $t ? rand ( 5, 18 ) : 1;
			$arc->setInc ( 'click', "id={$arr['id']}", $num );
		}
		unset ( $arc );
	
	} //end clickorshow
	

	/**页面右边调用函数**/
	
	/**
	 *每日之星
	 *@date 2010-12-16 / @time 下午02:46:54
	 */
	function site_of_day($day = '7',$type='arc' ,$limit = "0,1") {
		//每日之星
		$list=array();
		$list=self::_top_stat($day,$type,$limit);
		$data=array();
		$arc=new Model();
		$condition=array();
		$condition['arc.is_show']="1";
		$condition['lang.lang']=$this->lang;
		$condition['arc.'.$this->plang['l'].'name']=array('neq','');
		$xid='';
		foreach ($list as $t){
			$xid.=$t['xid'].',';
		}
		$condition['arc.id']=array('in',trim($xid,','));
		$field="arc.*,lang.url as url,lang.about as about";
		$data=$arc->Table('iic_arc arc')->join("iic_arc_lang lang On arc.id=lang.aid")->Field($field)->where($condition)->find();
		$this->assign('site_of_day',$data);
		unset($list,$data,$arc);
	} //end site_of_day
	

	/**
	 *精品推荐
	 *@date 2010-12-17 / @time 下午02:11:48
	 */
	function featured_link($limit = "0,10") {
		//精品推荐
		$arc=D("Arc");
		$condition=array();
		$condition['is_show']='1';
		$condition['_String']="(FIND_IN_SET('".$this->lang."',`olang`) > 0) AND (FIND_IN_SET('f',`attr`) > 0)";
		$list=$arc->where($condition)->order("activetime DESC")->limit("0,10")->findAll();
		$this->assign('featured_link',$list);
		unset($list,$arc);
	} //end featured_link
	

	/**
	 *最新的商业用户
	 *@date 2010-12-17 / @time 下午02:12:37
	 */
	function cool_link($limit = "0,10") {
		//最新的商业用户
		$arc=D("Arc");
		$condition=array();
		$condition['is_show']='1';
		$condition['_String']="(FIND_IN_SET('".$this->lang."',`olang`) > 0) AND vipstart<>0";
		$list=$arc->where($condition)->order("vipstart DESC")->limit("0,10")->findAll();
		$this->assign('cool_link',$list);
		unset($list,$arc);

	} //end cool_link
	
	/**
	   *我关注的
	   *@date 2010-12-29 / @time 下午03:51:50
	   */
	function my_focus() {
		//我关注的
		
	}//end my_focus
	
	/**
	   *我的查看记录
	   *@date 2010-12-29 / @time 下午03:54:38
	   */
	function my_history() {
		//我的查看记录
		
	}//end my_history
	
	/**
	   *我的收藏夹
	   *@date 2010-12-29 / @time 下午03:55:17
	   */
	function my_favorite() {
		//我的收藏夹
		
	}//end my_favorite
	
	/**
	   *更新各处点击量
	   *@date 2011-1-8 / @time 下午05:34:09
	   *@param type string arc product news deep album
	   *@param id int 资源ID
	   */
	function update_click($type='arc',$id) {
		//更新各处点击量
		switch (TRUE){
			case $type=='arc':
				break;
			case $type=='product':
				break;
			case $type=='news':
				break;
			case $type=='deep':
				break;
			case $type=='album':
				break;
			
		}
	}//end update_click
	
}//end class


/* CREATE TABLE `iic_session` (
`id` int(11) unsigned NOT NULL auto_increment,
`session_id` varchar(255) NOT NULL,
`session_expires` int(11) NOT NULL,
`session_data` blob,
PRIMARY KEY  (`id`)
)*/